19. Exercise: Change the Text
L1 45 Change The Text SC
In this exercise, you will make your app show a random number in the result_text TextView.
1. Assign an id to TextView in the layout:
android:id="@+id/result_text"
2. Remove the Toast and create a method called rollDice:
You can do this by adding the following:
rollButton.setOnClickListener {
rollDice()
}
And then using the keyboard shortcut, you can generate the method in Android Studio:
- Windows/Linux: Alt + Enter
- Mac: Option + Enter
3. Write have the rollDice method to get a random number between 1 and 6:
val randomInt = Random().nextInt(6) + 1
4. Use findViewById to get a reference to the TextView and assign it to an immutable variable called resultText:
val resultText: TextView = findViewById(R.id.result_text)
5. Finally, set the random value that you got above as the text of the TextView:
resultText.text = randomInt.toString()
If you want to start at this step, you can download this exercise code here: Step.03-Exercise-Rolling-the-dice.
You will find plenty of //TODO comments to help you complete this exercise, and if you get stuck, go back and watch the video again.
Once you’re done, you can check your solution against the solution we’ve provided here Step.03-Solution-Rolling-the-dice or using this git diff
Task Description:
Check the steps below as you implement them to complete this exercise.
Task Feedback:
Great job changing the text !
Solution: Step.03-Solution-Rolling-the-dice or using this git diff